package com.yuven.appframework.widget.titlebar

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.NonNull
import androidx.annotation.StringRes
import com.yuven.baselib.utils.ResourceUtil

/**
 * 
 *
 * @author :        Yuven
 * @date :          2021/7/30 9:25 上午
 * @desc :
 */
class StandardTitleBar : RelativeLayout, View.OnClickListener, ITitleBar {

    private var mTvTitle: TextView? = null
    private var mIvTitle: ImageView? = null
    private var mTvRight: TextView? = null
    private var mIvRight: ImageView? = null
    private var mTvLeft: TextView? = null
    private var mIvLeft: ImageView? = null
    private var mViewTitleBar:View? = null;

    private var  titleFactory: ITitleBar.Factory? = null
    private var clickListener: OnTileClickListener? = null

    constructor(context: Context?) : super(context!!) {
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
        context!!, attrs, defStyle
    ) {
        initView()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    ) {
        initView()
    }

    private fun initView(){
        setBackgroundColor(Color.TRANSPARENT)
        titleFactory = TitleBarConfig.titleBarConfig
        mViewTitleBar = titleFactory?.createTitleBarView(context);
        mViewTitleBar?.let {
            addView(mViewTitleBar,0)
            titleFactory?.getTitleBarHeight()?.let {
                mViewTitleBar?.minimumHeight = it
            }
            mTvTitle = titleFactory?.getTitleTextView(context)
            mIvTitle = titleFactory?.getTitleIcon(context)
            mTvLeft = titleFactory?.getLeftTextView(context)
            mIvLeft = titleFactory?.getLeftIcon(context)
            mTvRight = titleFactory?.getRightTextView(context)
            mIvRight = titleFactory?.getRightIcon(context)
        }
    }

    fun setTitleFactory(factory: ITitleBar.Factory){
        titleFactory = factory;
        removeAllViews();
        initView()
    }

    /**
     * 设置监听器
     */
    override fun setOnTitleBarListener(listener: OnTileClickListener) {
        clickListener = listener
        mTvTitle?.setOnClickListener(this)
        mIvTitle?.setOnClickListener(this)
        mTvLeft?.setOnClickListener(this)
        mIvLeft?.setOnClickListener(this)
        mTvRight?.setOnClickListener(this)
        mIvRight?.setOnClickListener(this)
    }


    /**
     * 设置创造器
     */
    fun setTitleBarFactory(titleFactory: ITitleBar.Factory) {
        this.titleFactory = titleFactory
    }

    override fun onClick(v: View?) {
        clickListener?.let {
            when (v) {
                mIvTitle -> {
                    clickListener?.onTitleIconClick(v)
                }
                mTvTitle -> {
                    clickListener?.onTitleClick(v)
                }
                mTvRight -> {
                    clickListener?.onRightClick(v)
                }
                mIvRight -> {
                    clickListener?.onRightIconClick(v)
                }
                mTvLeft -> {
                    clickListener?.onTitleClick(v)
                }
                mIvLeft -> {
                    clickListener?.onLeftIconClick(v)
                }
                else -> {

                }
            }
        }
    }

    /**
     * 设置标题
     */
    fun setTitle(@StringRes resId:Int) {
        setTitle(ResourceUtil.getString(resId))
    }

    /**
     * 设置标题
     */
    fun setTitle(title:CharSequence){
        mTvTitle?.text = title
    }

    /**
     * 设置左侧标题
     */
    fun setLeftTitle(title:CharSequence){
        mTvLeft?.text = title
    }

    /**
     * 设置左侧标题
     */
    fun setLeftTitle(@StringRes resId:Int) {
        setLeftTitle(ResourceUtil.getString(resId))
    }

    /**
     * 设置右侧侧标题
     */
    fun setRightTitle(title:CharSequence) {
        mTvRight?.text = title
    }

    /**
     * 设置右侧侧标题
     */
    fun setRightTitle(@StringRes resId:Int) {
        setRightTitle(ResourceUtil.getString(resId))
    }

    @NonNull
    fun setTitleBarColor(@ColorRes colorId:Int) {
        mViewTitleBar?.setBackgroundColor(ResourceUtil.getColor(colorId))
    }

    override fun getTitleTextView(): TextView? {
        return mTvTitle;
    }

    override fun getLeftIcon(): ImageView? {
        return mIvLeft
    }

    public override fun getLeftTextView():TextView?{
        return mTvLeft
    }

    public override fun getRightTextView():TextView?{
        return mTvRight
    }

    override fun getRightIcon(): ImageView? {
        return mIvRight
    }
}